<?php

use Illuminate\Database\Capsule\Manager as Capsule;

class Bootstrap extends Yaf\Bootstrap_Abstract
{

    public function _initLoader()
    {
        set_error_handler(
            create_function(
                '$severity, $message, $file, $line',
                'throw new ErrorException($message, $severity, $severity, $file, $line);'
            )
        );

        register_shutdown_function(array($this, 'cleanup'));

        Yaf\Loader::import(APP_PATH . "/vendor/autoload.php");
        Yaf\Loader::import(APP_PATH . "/application/function.php");

        // 注册本地类名前缀, 这部分类名将会在本地类库查找
        Yaf\Loader::getInstance()->registerLocalNameSpace(array('Log', 'Cache', 'Upload', 'Http', 'Util'));
    }

    public function _initConfig()
    {
        $config = Yaf\Application::app()->getConfig();
        Yaf\Registry::set('config', $config);
        Yaf\Dispatcher::getInstance()->autoRender(FALSE);  // 关闭自动加载模板  
    }

    public function _initDefaultName(Yaf\Dispatcher $dispatcher)
    {
        $dispatcher->setDefaultModule('Index')->setDefaultController('Index')->setDefaultAction('index');
    }

    public function _initDatabaseEloquent()
    {
        $capsule = new Capsule;

        // 创建默认链接
        $capsule->addConnection(Yaf\Application::app()->getConfig()->database->toArray());

        // biz业务链接
        // $capsule->addConnection(Yaf\Application::app()->getConfig()->biz->toArray(), 'biz');

        // 设置全局静态可访问
        $capsule->setAsGlobal();

        // 启动Eloquent
        $capsule->bootEloquent();

        // define('DT', 'dt');
        // $capsule::connection('dt')->enableQueryLog();

    }

    public function _initRoute(Yaf\Dispatcher $dispatcher)
    {
        return;
        $router = $dispatcher->getRouter();
        $router->addRoute('404', new Yaf\Route\Rewrite(
            '/404$',
            array(
                'controller'    => 'Public',
                'action'        => 'unknow'
            )
        ));
    }

    public function _initSession()
    {
        return;
        try {
            $redis = redisConnect();
            $redis->ping();
            $session = new Util_Session();
            session_set_save_handler($session, true);
        } catch (Exception $e) {
            Log_Log::info('[Bootstrap] session init error:' . $e->getMessage(), true, true);
        }
    }

    public function cleanup()
    {
        return;
    }

}